package com.shsun.curveservice.logic.service.impl;

import com.shsun.curveapi.domain.dto.AddCurveDTO;
import com.shsun.curveapi.domain.dto.CurveDetailDTO;
import com.shsun.curveservice.domain.entity.Curve;
import com.shsun.curveservice.domain.entity.CurveDetail;
import com.shsun.curveservice.logic.service.CurveDetailService;
import com.shsun.curveservice.logic.service.CurveMgrService;
import com.shsun.curveservice.logic.service.CurveService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * curve mgr service impl
 *
 * @author shsun
 * @date 2024/06/09
 */
@Service
public class CurveMgrServiceImpl implements CurveMgrService {
    @Resource
    private CurveService curveService;
    @Resource
    private CurveDetailService curveDetailService;
    /**
     * 添加曲线
     *
     * @param addCurveDTO add curve dto
     * @date 2024/06/09
     * @author shsun
     */
    @Override
    public void add(AddCurveDTO addCurveDTO) {
        Long userId = 5l;
        Curve curve = new Curve();
        curve.setName(addCurveDTO.getCurveName());
        curve.setUserId(userId);
        curveService.save(curve);
        List<CurveDetailDTO> list = addCurveDTO.getList();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (CurveDetailDTO curveDetailDTO : list) {
            CurveDetail curveDetail = new CurveDetail();
            curveDetail.setUserId(userId);
            curveDetail.setRuleId(curve.getId());
            curveDetail.setValue(curveDetailDTO.getValue());
            curveDetail.setUnit(curveDetailDTO.getUnit());
            curveDetail.setSort(curveDetailDTO.getSort());
            curveDetailService.save(curveDetail);
        }
    }
}
